package com.lczyfz.wisdom.common.utils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.lczyfz.wisdom.common.persistence.Page;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ServerResponse<T> implements Serializable {
    private static final long serialVersionUID = -2235155451651905167L;
    private int msgCode;
    private String errMsg;
    private T data;
    private List<T> dataList;
    private Page<T> page;
    private Date requestDateTime;
    private Date receiptDateTime;
    private Date returnDateTime;

    public void setData(T data) {
        this.data = data;
    }

    public ServerResponse(int msgCode) {
        this.msgCode = msgCode;
        this.errMsg = MsgCodeUtils.getErrMsg(msgCode);
        this.receiptDateTime = new Date();
    }

    public ServerResponse(int msgCode, String errMsg) {
        this.msgCode = msgCode;
        this.errMsg = errMsg;
        this.receiptDateTime = new Date();
    }

    public ServerResponse(int msgCode, String errMsg, T data) {
        this(msgCode, errMsg);
        this.data = data;
    }

    public ServerResponse(int msgCode, T data) {
        this(msgCode);
        this.data = data;
    }

    public ServerResponse(int msgCode, Page<T> page) {
        this(msgCode);
        this.page = page;
    }

    public ServerResponse(int msgCode, List<T> dataList) {
        this(msgCode);
        this.dataList = dataList;
    }

    public ServerResponse(int msgCode, String errMsg, List<T> dataList) {
        this(msgCode, errMsg);
        this.dataList = dataList;
    }

    public Date getRequestDateTime() {
        return requestDateTime;
    }

    public void setRequestDateTime(Date requestDateTime) {
        this.requestDateTime = requestDateTime;
    }

    public Date getReceiptDateTime() {
        return receiptDateTime;
    }

    public void setReceiptDateTime(Date receiptDateTime) {
        this.receiptDateTime = receiptDateTime;
    }

    public Date getReturnDateTime() {
        return returnDateTime;
    }

    public void setReturnDateTime(Date returnDateTime) {
        this.returnDateTime = returnDateTime;
    }

    public List<T> getDataList() {
        return this.dataList;
    }

    public void setDataList(List<T> dataList) {
        this.dataList = dataList;
    }

    public Page<T> getPage() {
        return this.page;
    }

    public void setPage(Page<T> page) {
        this.page = page;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.msgCode == ResponseCode.SUCCESS.getCode();
    }

    public int getMsgCode() {
        return this.msgCode;
    }

    public String getErrMsg() {
        return this.errMsg;
    }

    public T getData() {
        return this.data;
    }

    public static <E> ServerResponse<E> createBySuccess(E data) {
        return new ServerResponse(ResponseCode.SUCCESS.getCode(), data);
    }

    public static <E> ServerResponse<E> createBySuccessMessage(String msg) {
        return createServerResponse(ResponseCode.CREATE_SUCCESS.getCode(), msg);
    }

    public static <E> ServerResponse<E> deleteBySuccessMessage(String msg) {
        return createServerResponse(ResponseCode.DELETE_SUCCESS.getCode(), msg);
    }

    public static <E> ServerResponse<E> createByError() {
        return createServerResponse(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
    }

    public static <E> ServerResponse<E> createByErrorMessage(String errorMessage) {
        return createServerResponse(ResponseCode.ERROR.getCode(), errorMessage);
    }

    public static <E> ServerResponse<E> unLogin() {
        return createServerResponse(ResponseCode.UN_LOGIN.getCode(), MsgCodeUtils.getErrMsg(ResponseCode.UN_LOGIN.getCode()));
    }

    public static <E> ServerResponse<E> createByErrorCodeMessage(int errorCode, String errorMessage) {
        return createServerResponse(errorCode, errorMessage);
    }

    private static <E> ServerResponse<E> createServerResponse(int code, String message) {
        return new ServerResponse(code, message);
    }

    public void setMsgCode(int msgCode) {
        this.msgCode = msgCode;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }
}